using System;
using System.Xml;
using System.Collections;
using SharpObjects.Configuration;
using SharpObjects.Configuration.ConfigurationProviders;
using SharpObjects.Instantiation.Services;
using SharpObjects.PathMapping;

namespace SharpObjects.Logging.LogTargets 
{
	public class ConsoleLogTarget : LogTargetBase
	{
		protected string logName = DefaultPathMapper.ApplicationName;

		public ConsoleLogTarget()
		{
			CreateMutex(this.logName);
		}

		public override void Release()
		{
		}

		public override void Initialize(XmlElement initializationElement)
		{
            base.Initialize(initializationElement);

			IConfigurationProvider init = new XmlConfigurationProvider(initializationElement);
            this.formatter = init.GetObject<ILogFormatter>(formatterTag, (ILogFormatter)null);

            if (this.formatter == null)
            {
                string formatterId = init.GetString(formatter_id_tag);
                this.formatter = ServiceFactory.Instance.GetService<ILogFormatter>(formatterId);
            }
        }

		protected override void WriteToLog(ILogEntry logEntry)
		{
            string logMessage = this.formatter.ApplyFormat(logEntry);
            Console.Write(logMessage);
		}

        protected override void Close()
        {
        }

	}
}
